package com.jkoss.wine.warehouse.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.wine.warehouse.entity.WareInventory;
import com.jkoss.wine.warehouse.entity.WareOutOrder;
import com.jkoss.wine.warehouse.entity.WareOutOrderDetails;
import com.jkoss.wine.warehouse.mapper.WareInventoryMapper;
import com.jkoss.wine.warehouse.mapper.WareOutOrderDetailsMapper;
import com.jkoss.wine.warehouse.mapper.WareOutOrderMapper;
import com.jkoss.wine.warehouse.service.IWareOutOrderService;
import com.jkoss.wine.warehouse.vo.WareOutOrderVo;

/**
 * 出库单 服务实现类
 * 
 * @Author Jason
 * @Version 1.0, 2018-11-30
 * @See
 * @Since com.jkoss.wine.warehouse.service.impl
 * @Description: TODO
 */
@Service
public class WareOutOrderServiceImpl extends ServiceImpl<WareOutOrderMapper, WareOutOrder> implements IWareOutOrderService {
	@Autowired
	private WareOutOrderDetailsMapper wareOutOrderDetailsMapper;
	@Autowired
	private WareInventoryMapper wareInventoryMapper;

	@Override
	public boolean deleteOutOrder(List<WareOutOrder> list) throws Exception {
		for (WareOutOrder wareInoutOrder : list) {
			if (!retBool(baseMapper.updateById(wareInoutOrder))) {
				throw new Exception("删除出入库单失败");
			}
			Wrapper wrapper = Condition.create().eq("ioid", wareInoutOrder.getId()).ne("del", 1);
			List<WareOutOrderDetails> wareIODetailsList = wareOutOrderDetailsMapper.selectList(wrapper);
			for (WareOutOrderDetails wareInoutOrderDetails : wareIODetailsList) {
				// 减少库存
				WareInventory entity = new WareInventory();
				entity.setPnid(wareInoutOrderDetails.getPnid());
				entity.setWhid(wareInoutOrderDetails.getWhid());
				WareInventory wareInventory = wareInventoryMapper.selectOne(entity);
				wareInventory.setAmut(wareInventory.getAmut() + wareInoutOrderDetails.getAmut());
				if (!retBool(wareInventoryMapper.updateById(wareInventory))) {
					throw new Exception("减少库存失败");
				}
				// 删除订单商品
				wareInoutOrderDetails.setDel(1);
				if (!retBool(wareOutOrderDetailsMapper.updateById(wareInoutOrderDetails))) {
					throw new Exception("删除出入库商品失败");
				}
			}
		}
		return true;
	}

	@Override
	public boolean updateOutOrder(WareOutOrder inout_order, String wioodid[], String whid[], String pnid[], String amut[]) throws Exception {
		// 对订单详情表的操作
		if (!retBool(baseMapper.updateById(inout_order))) {
			throw new Exception("修改商品出入库单失败");
		}

		// 1.先把原来订单的数量恢复(还原订单数据)
		Wrapper wrapper = Condition.create().ne("del", 1).eq("ioid", inout_order.getId());
		List<WareOutOrderDetails> details = wareOutOrderDetailsMapper.selectList(wrapper);
		for (WareOutOrderDetails wareOutOrderDetails : details) {
			WareInventory oldentity = new WareInventory();
			oldentity.setPnid(wareOutOrderDetails.getPnid());
			oldentity.setWhid(wareOutOrderDetails.getWhid());
			// 查找旧仓库
			WareInventory oldWareInventory = wareInventoryMapper.selectOne(oldentity);
			// 减除原订单的数量
			oldWareInventory.setAmut(oldWareInventory.getAmut() + wareOutOrderDetails.getAmut());
			if (!retBool(wareInventoryMapper.updateById(oldWareInventory))) {
				throw new Exception("修改库存失败");
			}
			if (wareOutOrderDetailsMapper.deleteById(wareOutOrderDetails.getId()) < 1) {
				throw new Exception("修改库存失败");
			}
		}

		// // 添加修改库存
		// for (int i = 0; i < amut.length; i++) {
		// if (!CommonUtil.isBlank(wioodid[i])) {
		// WareOutOrderDetails wareInoutOrderDetails =
		// wareOutOrderDetailsMapper.selectById(wioodid[i]);
		// WareInventory oldentity = new WareInventory();
		// oldentity.setPnid(wareInoutOrderDetails.getPnid());
		// oldentity.setWhid(wareInoutOrderDetails.getWhid());
		// // 查找旧仓库
		// WareInventory oldWareInventory = wareInventoryMapper.selectOne(oldentity);
		// // 减除原订单的数量
		// oldWareInventory.setAmut(oldWareInventory.getAmut() +
		// wareInoutOrderDetails.getAmut());
		// if (!retBool(wareInventoryMapper.updateById(oldWareInventory))) {
		// throw new Exception("修改库存失败");
		// }
		// // 查找新仓库
		// WareInventory newentity = new WareInventory();
		// newentity.setPnid(pnid[i]);
		// newentity.setWhid(whid[i]);
		// WareInventory newWareInventory = wareInventoryMapper.selectOne(newentity);
		// if (!CommonUtil.isBlank(newWareInventory)) {
		// newWareInventory.setAmut(newWareInventory.getAmut() -
		// CommonUtil.int2(amut[i]));
		// wareInventoryMapper.updateById(newWareInventory);
		// } else {
		// newWareInventory = new WareInventory();
		// newWareInventory.setAmut(-CommonUtil.int2(amut[i]));
		// // 产品规格id
		// newWareInventory.setPnid(pnid[i]);
		// // 仓库id
		// newWareInventory.setWhid(whid[i]);
		// // 添加时间
		// newWareInventory.setAddTime(CommonUtil.getDate());
		// // 是否删除 0-否 1-是 2-不能删除
		// newWareInventory.setDel(0);
		// wareInventoryMapper.insert(newWareInventory);
		// }
		//
		// // 修改商品详情
		// wareInoutOrderDetails.setAmut(CommonUtil.int2(amut[i]));
		// wareInoutOrderDetails.setPnid(pnid[i]);
		// wareInoutOrderDetails.setWhid(whid[i]);
		// if (!retBool(wareOutOrderDetailsMapper.updateById(wareInoutOrderDetails))) {
		// throw new Exception("修改库存商品信息失败");
		// }
		// } else {
		// // 修改库存
		// WareInventory entity = new WareInventory();
		// entity.setPnid(pnid[i]);
		// entity.setWhid(whid[i]);
		// WareInventory wareInventory = wareInventoryMapper.selectOne(entity);
		// if (!CommonUtil.isBlank(wareInventory)) {
		// wareInventory.setAmut(wareInventory.getAmut() - CommonUtil.int2(amut[i]));
		// if (!retBool(wareInventoryMapper.updateById(wareInventory))) {
		// throw new Exception("修改库存失败");
		// }
		// } else {
		// wareInventory = new WareInventory();
		// // 商品规格id
		// wareInventory.setPnid(pnid[i]);
		// // 仓库id
		// wareInventory.setWhid(whid[i]);
		// // 数量
		// wareInventory.setAmut(-CommonUtil.int2(amut[i]));
		// // 添加时间
		// wareInventory.setAddTime(CommonUtil.getDate());
		// // 是否删除 0-否 1-是 2-不能删除
		// wareInventory.setDel(0);
		// if (!retBool(wareInventoryMapper.insert(wareInventory))) {
		// throw new Exception("修改库存失败");
		// }
		// }
		//
		// // 添加商品
		// WareOutOrderDetails details = new WareOutOrderDetails();
		// details.setAmut(CommonUtil.int2(amut[i]));
		// // 商品规格id
		// details.setPnid(pnid[i]);
		// // 仓库id
		// details.setWhid(whid[i]);
		// // 出入库单号
		// details.setIoid(inout_order.getId());
		// // 是否删除 0-否 1-是 2-不能删除
		// details.setDel(0);
		// // 添加时间
		// details.setAddTime(CommonUtil.getDate());
		// if (!retBool(wareOutOrderDetailsMapper.insert(details))) {
		// throw new Exception("添加库存商品信息失败");
		// }
		// }
		// }

		// 对库存表的操作
		List<WareOutOrderDetails> list = new ArrayList<>();
		// 对库存表的操作
		List<WareInventory> list2 = new ArrayList<>();
		for (int j = 0; j < whid.length; j++) {
			if (!CommonUtil.isBlank(amut[j])) {
				WareOutOrderDetails orderDetails = new WareOutOrderDetails();
				// 仓库id
				orderDetails.setWhid(whid[j]);
				// 商品规格id
				orderDetails.setPnid(pnid[j]);
				// 商品数量
				orderDetails.setAmut(CommonUtil.int2(amut[j]));
				// 进入库id
				orderDetails.setIoid(inout_order.getId());
				// 当前用户id
				orderDetails.setOperator(inout_order.getOperator());
				// 添加时间
				orderDetails.setAddTime(CommonUtil.getDate());
				// 是否删除 0-未删除 1-已删除
				orderDetails.setDel(0);
				list.add(orderDetails);
			}
		}
		if (!retBool(wareOutOrderDetailsMapper.insertBatch(list))) {
			throw new Exception("添加商品库存详情失败");
		}
		// 对库存表的操作
		for (int i = 0; i < whid.length; i++) {
			if (!CommonUtil.isBlank(amut[i])) {
				WareInventory wareInventory = new WareInventory();
				// 仓库id
				wareInventory.setWhid(whid[i]);
				// 商品规格id
				wareInventory.setPnid(pnid[i]);
				wareInventory = wareInventoryMapper.selectOne(wareInventory);
				// 商品是否已存在库存
				boolean isinsert = false;
				if (CommonUtil.isBlank(wareInventory)) {
					isinsert = true;
					wareInventory = new WareInventory();
				}
				// 仓库id
				wareInventory.setWhid(whid[i]);
				// 商品规格id
				wareInventory.setPnid(pnid[i]);
				// 订单类型：1-出库、2-入库
				// if (inout_order.getType() == 1) {
				// // 商品数量
				// wareInventory.setAmut(CommonUtil.int2(wareInventory.getAmut()) -
				// CommonUtil.int2(amut[i]));
				// } else if (inout_order.getType() == 2) {
				// // 商品数量
				// wareInventory.setAmut(CommonUtil.int2(wareInventory.getAmut()) +
				// CommonUtil.int2(amut[i]));
				// }
				// 商品数量
				wareInventory.setAmut(CommonUtil.int2(wareInventory.getAmut()) - CommonUtil.int2(amut[i]));
				// 当前操作人
				wareInventory.setOperator(inout_order.getOperator());
				// 添加时间
				wareInventory.setAddTime(CommonUtil.getDate());
				// 是否删除 0-否 1-是 2-不能删除
				wareInventory.setDel(0);

				if (isinsert) {
					if (!retBool(wareInventoryMapper.insert(wareInventory))) {
						throw new Exception("添加商品库存失败");
					}
				} else {
					if (!retBool(wareInventoryMapper.updateById(wareInventory))) {
						throw new Exception("修改商品库存失败");
					}
				}

			}
		}
		return true;
	}

	@Override
	public boolean insertOutOrder(WareOutOrder inout_order, String whid[], String pnid[], String amut[]) throws Exception {
		// 对订单详情表的操作
		if (!retBool(baseMapper.insert(inout_order))) {
			throw new Exception("添加商品出入库单失败");
		}
		// 对库存表的操作
		List<WareOutOrderDetails> list = new ArrayList<>();
		// 对库存表的操作
		List<WareInventory> list2 = new ArrayList<>();
		for (int j = 0; j < whid.length; j++) {
			if (!CommonUtil.isBlank(amut[j])) {
				WareOutOrderDetails orderDetails = new WareOutOrderDetails();
				// 仓库id
				orderDetails.setWhid(whid[j]);
				// 商品规格id
				orderDetails.setPnid(pnid[j]);
				// 商品数量
				orderDetails.setAmut(CommonUtil.int2(amut[j]));
				// 进入库id
				orderDetails.setIoid(inout_order.getId());
				// 当前用户id
				orderDetails.setOperator(inout_order.getOperator());
				// 添加时间
				orderDetails.setAddTime(CommonUtil.getDate());
				// 是否删除 0-未删除 1-已删除
				orderDetails.setDel(0);
				list.add(orderDetails);
			}
		}
		if (!retBool(wareOutOrderDetailsMapper.insertBatch(list))) {
			throw new Exception("添加商品库存详情失败");
		}
		// 对库存表的操作
		for (int i = 0; i < whid.length; i++) {
			if (!CommonUtil.isBlank(amut[i])) {
				WareInventory wareInventory = new WareInventory();
				// 仓库id
				wareInventory.setWhid(whid[i]);
				// 商品规格id
				wareInventory.setPnid(pnid[i]);
				wareInventory = wareInventoryMapper.selectOne(wareInventory);
				// 商品是否已存在库存
				boolean isinsert = false;
				if (CommonUtil.isBlank(wareInventory)) {
					isinsert = true;
					wareInventory = new WareInventory();
				}
				// 仓库id
				wareInventory.setWhid(whid[i]);
				// 商品规格id
				wareInventory.setPnid(pnid[i]);
				// 订单类型：1-出库、2-入库
				if (inout_order.getType() == 1) {
					// 商品数量
					wareInventory.setAmut(CommonUtil.int2(wareInventory.getAmut()) - CommonUtil.int2(amut[i]));
				} else if (inout_order.getType() == 2) {
					// 商品数量
					wareInventory.setAmut(CommonUtil.int2(wareInventory.getAmut()) + CommonUtil.int2(amut[i]));
				}
				// 当前操作人
				wareInventory.setOperator(inout_order.getOperator());
				// 添加时间
				wareInventory.setAddTime(CommonUtil.getDate());
				// 是否删除 0-否 1-是 2-不能删除
				wareInventory.setDel(0);

				if (isinsert) {
					if (!retBool(wareInventoryMapper.insert(wareInventory))) {
						throw new Exception("添加商品库存失败");
					}
				} else {
					if (!retBool(wareInventoryMapper.updateById(wareInventory))) {
						throw new Exception("修改商品库存失败");
					}
				}

			}
		}
		return true;
	}

	@Override
	public Page selectVoByioid(Page page, Wrapper wrapper) {
		SqlHelper.fillWrapper(page, wrapper);
		page.setRecords(baseMapper.selectVoByioid(page, wrapper));
		return page;
	}

	@Override
	public WareOutOrderVo selectVoById(String id) {
		return baseMapper.selectVoById(id);
	}

	@Override
	public Page selectVoExcel(Page page, Wrapper wrapper) {
		SqlHelper.fillWrapper(page, wrapper);
		page.setRecords(baseMapper.selectVoExcel(page, wrapper));
		return page;
	}

}
